package task4;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author Stranger
 * @Date 2021/10/15 12:49
 * @Description This is description of method
 * @Param
 * @Return
 * @Since 三元组顺序表表示的稀疏矩阵加法
 */
public class Task5 {
    static Scanner input=new Scanner(System.in);
    static ArrayList<ArrayList<Integer>> list=new ArrayList<>();
    public static void main(String[] args) {
        int m=input.nextInt();//行
        int n=input.nextInt();//列
        int t1=input.nextInt();//A的非零个数
        int t2=input.nextInt();//B的非零个数


        for(int i=0;i<t1+t2;i++)//稀疏数组A录入数据
        {
            int col=input.nextInt();//非零元的行 列 值
            int row=input.nextInt();
            int value=input.nextInt();

            ArrayList<Integer> arrayList=new ArrayList<>();

            arrayList.add(col);
            arrayList.add(row);
            arrayList.add(value);
            list.add(arrayList);
        }
        matrixAddition(t1,t2);
        print(m,n);
    }

    private static void print(int m, int n) {
        System.out.println(m+" "+n+" "+list.size());//行列以及非零元的个数
        for(ArrayList<Integer> innerList: list)
        {
                System.out.println(
                        innerList.get(0)+" "+
                        innerList.get(1)+" "+
                        innerList.get(2));
        }
    }

    private static void matrixAddition(int t1,int t2){
        for(int i=0;i<t1;i++)
        {
            int colA=list.get(i).get(0);
            int rowA=list.get(i).get(1);
            int valA=list.get(i).get(2);
            for(int j=t1;j<list.size();j++)
            {
                int colB=list.get(j).get(0);
                int rowB=list.get(j).get(1);
                int valB=list.get(j).get(2);

                if(colA==colB&&rowA==rowB)
                {
                    list.get(i).set(2,valA+valB);
                    list.remove(list.get(j));
                    if(valA+valB==0)
                    {
                        list.remove(list.get(i));
                        t1--;
                    }
                    break;
                }
            }
        }

    }
}
